package cn.school.controller.publicPhone;


import cn.school.common.annotation.SysLog;
import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.enums.BusinessType;
import cn.school.common.exception.ApiException;
import cn.school.student.entity.StudentContacts;
import cn.school.publicPhone.service.StudentContactsService;
import cn.school.student.vo.StudentContactsExportVO;
import cn.school.student.vo.StudentContactsImportVO;
import cn.school.student.vo.StudentContactsVO;
import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 学生联系人
 */
@RestController
@RequestMapping("/controller")
public class StudenttContactsController {

    @Resource
    private StudentContactsService studentContactsService;
    private static Boolean lockByExcelImport = false;

    @PostMapping("/pageList")
    @ApiOperation("pageList=>分页查询学生联系人")
    public CommonResult<CommonPage<StudentContacts>> queryByPage(@RequestBody StudentContactsVO vo) {
        CommonPage<StudentContacts> pageList =  studentContactsService.getStudentContactsPage(vo);
        return CommonResult.success(pageList);
    }

    @PostMapping("/edit")
    @ApiOperation("pageList=>分页查询学生联系人")
    public CommonResult<Integer> addOrUpdateStudentContacts(@RequestBody StudentContacts vo) {
        int num =  studentContactsService.addOrUpdateStudentContacts(vo);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @PostMapping("/delete")
    @ApiOperation("pageList=>分页查询学生联系人")
    public CommonResult<Integer> deleteContactsById(@RequestBody  StudentContacts vo) {
        int num =  studentContactsService.deleteContactsById(vo.getId());
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("importTemplate => 下载模板")
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        StudentContactsImportVO vo = new StudentContactsImportVO();
        vo.setStudentName("学生姓名");
        vo.setClassName("班级名称");
        vo.setContactsName("联系人名称");
        vo.setContactsPhone("联系人号码");
        List<StudentContactsImportVO> list = new ArrayList<>();
        list.add(vo);
        EasyExcel.write(response.getOutputStream(), StudentContactsImportVO.class).sheet("学生联系人").doWrite(list);
    }

    @ApiOperation("export => 按条件导出（不分页）")
    @SysLog(title = "用户管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public CommonResult export(HttpServletResponse response, @RequestParam(required = false) Map<String,Object> map) {
        List<StudentContactsExportVO> listVo = new ArrayList<>();
        StudentContactsVO vo = new StudentContactsVO();
        if(!StringUtils.isEmpty(map.get("contactsPhone"))){
            vo.setContactsPhone((String)map.get("contactsPhone"));
        }
        if(!StringUtils.isEmpty(map.get("contactsName"))){
            vo.setContactsName((String)map.get("contactsName"));
        }
        if(!StringUtils.isEmpty(map.get("studentName"))){
            vo.setStudentName((String)map.get("studentName"));
        }


        List<StudentContacts> list = studentContactsService.findContactsList(vo);
        if (list != null && list.size() != 0) {
            StudentContactsExportVO exportVO = null;
            for (StudentContacts contacts : list) {
                exportVO = new StudentContactsExportVO();
                BeanUtils.copyProperties(contacts, exportVO);
                listVo.add(exportVO);
            }
        }
        try {
            EasyExcel.write(response.getOutputStream(), StudentContactsExportVO.class).sheet("学生联系人").doWrite(listVo);
            return CommonResult.success();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }

    @ApiOperation("导入联系人")
    @PostMapping("/importData")
    public CommonResult<String> importStudentFromExcel(@RequestParam("file") MultipartFile multipartFile) throws IOException, ApiException {
        synchronized (lockByExcelImport) {
            if (this.lockByExcelImport) {
                new ApiException("有教员用户导入正在处理，请稍后重试");
            } else {
                this.lockByExcelImport = true;
            }
        }

        try {
            return this.studentContactsService.importContactsList(multipartFile.getInputStream());
        } catch (ApiException apiException) {
            throw apiException;
        } catch (Exception e) {
            throw new ApiException("导入教员用户失败，请联系管理员");
        } finally {
            synchronized (lockByExcelImport) {
                this.lockByExcelImport = false;
            }
        }

    }
}
